<?php
/**
 *
 */

class cinema_brief_widget{

//    生成每个月报表数据
    public function cinema_month_order($give_time){
        //指定月影院进货
        $give_month = $this->getTheMonth($give_time);
        $month = array(
            'confirm_payoff_time >=' => $give_month[0],
            'confirm_payoff_time <=' => $give_month[1],
            'status' => 1,
            'is_payoff' => 1);

        $all_cinema_list = _model('store')->getList(array('type' => array(100, 105)));
        foreach($all_cinema_list as $k=>$v){
            //获取上个月的订单量
            $month['store_id'] = $v['id'];
            $month_cinema_order = _model('order')->getList($month);

            //指定月份进货金额
            $month_cinema_order_money_count = 0;
            foreach($month_cinema_order as $mk => $mv){
                $month_cinema_order_money_count += (int)$mv['payoff_money'];
            }

            //指定月份票房
            if($v['zzb_id']){
                $box_office_month = array(
                    'zzb_code' => $v['zzb_id'],
                    'box_office_month >=' => $give_month[0],
                    'box_office_month <=' => $give_month[1],
                    'is_enable' => 1);
                $box_office = _model('cinema_box_office') -> getList($box_office_month);
                $month_cinema_box_office_count = $box_office['box_office'];
            } else {
                $month_cinema_box_office_count = -1;
            }
        }
    }

    //生成每天的报表数据
    public function cinema_daily_order($date) {
        if (!$date) {
            $date = strtotime('-2 day'); //前2天的数据
        }
        $date = date('Y-m-d', $date);
        //判断是否已经有数据了
        $sum = _model('cinema_daily_box_office')->getTotal(array('box_office_date' => $date));
        if ($sum) {
            echo 'EXISTS    ::' . $date . "\n";
            return;
        }

        $res = $this->get_cinema_daily_report_by_curl($date, 1);
        if ($res['success'] == 1) {
            echo $res['info'];
            return ;
        }

        for ($i = 2; $i <= $res['pagenum']; $i++) {
            $this->get_cinema_daily_report_by_curl($date, $i);
        }

        $store_filter = array(
            'type' => '100',
            'is_enable' => 1,
            'is_delete' => 1,
            'status !=' => 0
        );
        $all_cinema_list = _model('store')->getList(enable_filter($store_filter));
        foreach ($all_cinema_list as $k=>$v) {
            $order_filter = array(
                'store_id' => $v['id'],
                'receiver_time >=' => $date . ' 00:00:00',
                'receiver_time <=' => $date . ' 23:59:59',
                'status' => 1,
                'is_enable' => 1,
                'progress' => 10
            );
            //指定日期进货金额
            $month_cinema_order_money_count = _model('order')->getFields('price', enable_filter($order_filter));
            $month_cinema_order_money_count = $month_cinema_order_money_count ? format_money(array_sum($month_cinema_order_money_count)) : 0.00;
            //指定日期票房、观影人数
            $month_cinema_box_office_count = null;
            $month_cinema_audience = null;
            if ($v['zzb_id']) {
                $box_office_date = array(
                    'zzb_code' => $v['zzb_id'],
                    'box_office_date =' => $date,
                    'is_enable' => 1
                );
                $box_office = _model('cinema_daily_box_office')->field('box_office', 'audience')->read($box_office_date);
                if ($box_office) {
                    $month_cinema_box_office_count = format_money($box_office['box_office']);
                    $month_cinema_audience = $box_office['audience'];
                }
            }
            _model('cinema_daily_report')->create(array(
                'store_id' => $v['id'],
                'date' => $date,
                'box_office' => $month_cinema_box_office_count,
                'viewers' => $month_cinema_audience,
                'order_money' => $month_cinema_order_money_count,
                'spp' => ($month_cinema_box_office_count > 0) ? ((float)$month_cinema_order_money_count / ((float)$month_cinema_audience)) : null,
                'province' => $v['province_id']
            ));
        }
        echo 'SUCCESS   ::' . $date . "\n";
    }

    private function get_cinema_daily_report_by_curl($date, $page) {
        $ndate = str_replace('-', '', $date);

        $authority_key = 'FN6LQQtZz3VprtbrW377gDatjBlskVPyuMuvXc5Q';
        $parameter = array();
        $parameter['ymd'] = $ndate;
        $parameter['page'] = $page;
        ksort($parameter);

        $str = '';
        foreach ($parameter as $k => $v) {
            if ($v) $str .= "$k=$v&";
        }
        $str = substr($str, 0, -1); // 去掉最后一个&
        $sign = md5($str . $authority_key);

        $parameter['sign'] = $sign;
        $url = 'http://a.cinemasmedia.cn/api/bbt-gjdyzjb_cinema_read.do';
        $res = curl_request($url, 'post', $parameter);
        if (!$res) {
            $info = 'CRUL DATA IS NULL ::' . $date . "\n";
            return array('success' => 1, 'info' => $info);
        }
        $res = json_decode($res, 1);
        if ($res['code'] != 0) {
            $info = $res['message'] . ' ::' . $date . "\n";
            return array('success' => 1, 'info' => $info);
        }

        $total = $res['result']['total'];
        $pagenum = ceil($total / 50);

        $result = $res['result']['data'];
        if (!$result) {
            $info = 'NO RECORD ::' . $date . "\n";
            return array('success' => 1, 'info' => $info);
        }

        foreach ($result as $value) {
            _model('cinema_daily_box_office')->create(array(
                'zzb_code' => $value['cinemacode'],
                'box_office' => format_money($value['boxoffice_total']),
                'audience' => $value['person_total'],
                'countshow' => $value['play_num'],
                'box_office_date' => $date
            ));
        }

        return array('success' => 0, 'info' => '', 'pagenum' => $pagenum);
    }

    /**
     * 导出excel发送邮件
     */
    public function export_list_email()
    {
        // 导出
//        _widget('admin_data.data')->export_list(0);
        $url = STATIC_URL .'/images/data/'.basename($this->path);
        // 给客户发一封
        if (ONDEV) {
            _widget('email')->mail(email_config::$coder_email_list, '抱抱堂运营数据', '下载地址： '.$url);
        } else {
            _widget('email')->mail(email_config::$user_email_list, '抱抱堂运营数据', '下载地址： '.$url);
        }
    }

    /**
     * 按照时间返回对应的日，周，月，统计用
     * @param unknown_type $type
     * @param unknown_type $time
     * @return string
     */
    private function get_date($type, $time = '')
    {
        if (!$time)  $time = time();

        if ($type == 'day') {
            $date = date('Y-m-d', $time);
        } else if ($type == 'week') {
            // 第几周
            $date = strftime('%Y%W', $time);
        } else if ($type == 'month') {
            $date = date('Ym', $time);
        }
        return $date;
    }


    //返回上个月时间段
    private function getLastMonth(){
        $time = time();
        $firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
        return array($firstday, $lastday);
    }

    //获取指定日期所在月的第一天和最后一天
    function getTheMonth($date){
        $firstday = date("Y-m-01",strtotime($date));
        $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
        return array($firstday,$lastday);
    }
}
?>
